
# 安装【opencv-python==4.10.0.84;pyperclip==1.9.0;pyzbar==0.1.9】
# 1前摄扫描二维码，按esc完成。

import cv2
import pyzbar.pyzbar as pyzbar
import pyperclip as ppr
def jietu():
    cap = cv2.VideoCapture(0,cv2.CAP_DSHOW)
    if not cap.isOpened():
        return None
    while (1):#True:【改为（1）就成功！！】
        ok,frame = cap.read()
        frame = cv2.flip(frame, 1)
        if not ok:
            break
        cv2.imshow('frame',frame)
        key = cv2.waitKey(10)  & 0xFF
        if key == 27 or cv2.getWindowProperty('frame', cv2.WND_PROP_VISIBLE) < 1.0: 
            cv2.imwrite("test00000.png", frame)
            break
    cap.release()
    cv2.destroyAllWindows()
def jiema():
    #2. 解析二维码

    imagePath = "test00000.png" #代码目录下，截取的test.png
    srcImg = cv2.imread(imagePath) #读取test.png
    barcodes = pyzbar.decode(srcImg)
    for barcode in barcodes:
        global barcodeData 
        barcodeData = barcode.data.decode("utf-8")
        # 对二维码信息进行解码，"utf-8"编码格式

        print(barcodeData)#【无则打印空】
        # 打印二维码识别的结构
def fuzhi():
    try:
        ppr.copy(barcodeData)  # 第二步中的barcodeData
    except :
        print('未识别到!!!')
if __name__ == "__main__":
    jietu()
    jiema()
    fuzhi()
